package com.axx.android.letter.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.maps.utils.SpatialRelationUtil;
import com.amap.api.maps.utils.overlay.SmoothMoveMarker;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.letter.model.LetterDetail;
import com.axx.android.letter.model.ResetPathResult;
import com.axx.android.utils.HttpUtils;
import com.axx.android.utils.ImageLoader;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LetterMapActivity extends BaseActivity implements AMap.OnMarkerClickListener {

    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.map)
    MapView map;
    @Bind(R.id.layout_toolbar)
    RelativeLayout layoutToolbar;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.iv_from)
    ImageView ivFrom;
    @Bind(R.id.tv_from)
    TextView tvFrom;
    @Bind(R.id.tv_no)
    TextView tvNo;
    @Bind(R.id.iv_to)
    ImageView ivTo;
    @Bind(R.id.tv_to)
    TextView tvTo;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.layout_letter_window)
    LinearLayout layoutLetterWindow;

    private ResetPathResult model;
    private LetterDetail detail;
    private String role;
    private AMap aMap;
    private ArrayList<LatLng> latLngList;


    @Override
    public int getLayoutId() {
        return R.layout.activity_letter_map;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        map.onCreate(savedInstanceState);
        aMap = map.getMap();
        aMap.setOnMarkerClickListener(this);
        role = getIntent().getStringExtra("role");
        model = (ResetPathResult) getIntent().getSerializableExtra("model");
        detail = (LetterDetail) getIntent().getSerializableExtra("detail");
        initLocation();
    }

    private void initLocation() {
        if (TextUtils.isEmpty(model.getPointLine()))
            return;

        latLngList = new ArrayList<>();
        String[] points = model.getPointLine().split(";");
        for (String point : points) {
            if (!TextUtils.isEmpty(point)) {
                String[] child = point.split(",");
                latLngList.add(new LatLng(Double.valueOf(child[1]), Double.valueOf(child[0])));
            }

        }
        latLngList.add(0, new LatLng(model.getNowLatitude(), model.getNowLongitude()));
        if ("sender".equals(role)) {
//            LatLng from = new LatLng(detail.getFromLatitude(), detail.getFromLongitude());
//            LatLng to = new LatLng(detail.getToLatitude(), detail.getToLongitude());
            movieCenter();
            addLine();
            moviePoint();
//            if (from.equals(to)) {
//                movieCenter();
//                moviePoint();
//            } else {
//
//            }
        } else {
            movieCenter();
            addLine();
            moviePoint();
        }
    }


    private void addLine() {
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.addAll(latLngList);
        polylineOptions.width(30).color(Color.parseColor("#45cc61"));
        aMap.addPolyline(polylineOptions);
    }

    private void moviePoint() {
        HttpUtils.getBitmap(detail.getTools().getPic(), 32, new HttpUtils.SJBitmapCallBack() {
            @Override
            public void onSuccess(Bitmap bitmap) {
                SmoothMoveMarker smoothMarker = new SmoothMoveMarker(aMap);
                smoothMarker.setDescriptor(BitmapDescriptorFactory.fromBitmap(bitmap));

                smoothMarker.setPoints(latLngList);
                smoothMarker.setTotalDuration(model.getCostSecond());
                smoothMarker.startSmoothMove();
            }
        });
    }

    private void movieCenter() {
        LatLng v1 = latLngList.get(0);
        aMap.animateCamera(CameraUpdateFactory.newLatLng(v1));
    }


    @Override
    public boolean onMarkerClick(Marker marker) {
//        tvNo.setText("No" + detail.getTrains() + "次");
//        tvNum.setText(detail.getCode());
//        tvContent.setText(detail.getText());
//        ImageLoader.loadImage(detail.getFromUser().getHeadPic(), ivFrom, this);
//        ImageLoader.loadImage(detail.getToUser().getHeadPic(), ivTo, this);
//        tvFrom.setText(detail.getFromUser().getNickName());
//        tvTo.setText(detail.getToUser().getNickName());
//        layoutLetterWindow.setVisibility(View.VISIBLE);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
